<?php


namespace app\common\library;

use think\facade\Log;

class Yunpian
{

    public static function send($apikey,$tpl_id,$mobile, $text)
    {
        $data = array('tpl_value' => urlencode('#code#') . '=' . urlencode($text), 'apikey' => $apikey, 'mobile' => $mobile, 'tpl_id' => $tpl_id);
        $ch = curl_init();
        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
//        echo 'result';
//        dump($result);
//        echo 'error';
//        dump($error);
//        exit;
        $array = json_decode($result, true);
        //dump($array);
        Log::write($result, 'YUNPIAN');
        Log::write($array, 'YUNPIAN');
        Log::write(json_encode($error), 'YUNPIAN');
        return $array['code'] == 0 ? '0' : $array['msg'];
    }


}
